<template>
	<view class="page">
		<z-paging ref="paging" v-model="dataList" @query="queryList">
			<template #top>
				<u-navbar leftText="话题列表" safeAreaInsetTop fixed placeholder :autoBack="true"></u-navbar>
				<view style="padding: 20rpx 32rpx 32rpx 32rpx;background: #fff;">
					<view style="background: #f2f2f2;border-radius: 24rpx;">
						<view style="display: flex;align-items: center;">
							<!-- <image src="@/static/img/ss.png" mode="aspectFill" style="width: 40rpx;height: 40rpx;"></image> -->
							<!-- <view style=""> -->
								<!-- <input type="text" placeholder="请搜索" @ /> -->
								<u-search placeholder="请搜索" v-model="keyword" :show-action="false"
								@search="onSearch"></u-search>
								<!-- <u-search :showAction="true" actionText="请搜索" :animation="true"></u-search> -->
							<!-- </view> -->
						</view>
					</view>
				</view>
			</template>
			
			<!-- 列表 -->
			<view style="">
				<view style="padding: 24rpx 32rpx 0  32rpx;margin-bottom: 20rpx;" v-for="(item, index) in dataList" :key="index">
					<view style="border-bottom: #ccc solid 1px;padding-bottom: 24rpx;">
						
						<view class="flex" style="display: flex;margin-top: 16rpx;align-items: center;">
							<view class="">
								<image :src="item.image" mode="aspectFill" style="width: 50px;height: 50px;border-radius: 50%;
								margin-right: 20rpx;"></image>
							</view>
							<view class="c" style="flex: 1;font-size: 28rpx;">
								<view>{{item.name}}</view>
								<!-- <view style="font-size: 24rpx;color: #999;margin-top: 10rpx;">
									<view>{{item.follow_sum}}人关注 {{item.article_sum}}篇文章</view>
								</view> -->
							</view>
							<view style="font-size: 28rpx;">
								<view v-if="isTagSelected(item)" style="color: #fa3534;border: 2rpx solid #fa3534;
								border-radius: 60rpx;padding: 10rpx 0rpx;width: 60px;
								text-align: center;" @click="toggleTagSelect(item)">取消</view>
								<view v-else style="color: #1877f2;border: 2rpx solid #1877f2;
								border-radius: 60rpx;padding: 10rpx 0rpx;width: 60px;
								text-align: center;" @click="toggleTagSelect(item)">添加</view>
							</view>
						</view>
				
					</view>
				</view>
			</view>
			
			<template #bottom>
				<view class="footer">
					<button class="confirm-btn" @click="confirmSelection" style="background: #1877f2;color: #fff;">确定</button>
				</view>
			</template>
		</z-paging>
		
		<u-toast ref="uToast" />
		
		
		<view style="height: 200rpx;"></view>
		<!-- <view style="width: 100%;position: fixed;bottom: 0;">
			<view style="background: #fff;display: flex;align-items: center;justify-content: center;
			padding: 34rpx 34rpx 54rpx 34rpx;font-size: 32rpx;">
				<view style="color: #fff;background: #1877f2;
				padding: 20rpx 0;border-radius: 60rpx;width: 100%;text-align: center;"
				@click="onSubmitSave">+ 新增常用药品</view>
			</view>
		</view> -->
		
		<!-- <view style="position: fixed;right: 32rpx;bottom: 204rpx;" @click="jumpTo('/pages/prescription/ordinaryEdit')">
			<image src="@/static/img/add.png" mode="widthFix" style="width: 80rpx;"></image>
		</view> -->
		<!-- 更多操作 -->
		<u-action-sheet :list="moreList" v-model="moreShow" border-radius="32"
		@click="moreClick"></u-action-sheet>
		
	</view>
</template>

<script>
	import { request } from 'utils/request.js';
	export default {
		data() {
			return {
								
				dataList: [],
				keyword:'',
				
				allTags: [
				        { id: 1, name: '科技' },
				        { id: 2, name: '体育' },
				        { id: 3, name: '娱乐' },
				        { id: 4, name: '财经' },
				        { id: 5, name: '时尚' },
				        { id: 6, name: '美食' },
				        { id: 7, name: '旅游' },
				        { id: 8, name: '健康' },
				        { id: 9, name: '教育' },
				        { id: 10, name: '汽车' }
				      ], // 所有可用标签
				      selectedTags: [], // 当前已选标签
				      searchText: '', // 搜索文本
				      filteredTags: [] // 过滤后的标签列表
				
			};
		},
		onLoad() {
			// 告诉页面需要重新获取积分
			// uni.$on('againScore', (e) => {
			// 	this.getUserScore();
			// 	console.log('子页面---->>>>>>', e);
			// })
			// this.getArticleClassList();
			
		},
		onLoad(options) {
		    // 从父页面传递的已选标签
		    if (options.selected) {
				this.selectedTags = JSON.parse(options.selected);
		    }
		    // this.filteredTags = [...this.allTags];
		},
		methods: {
			// 搜索标签
			onSearch() {
				this.$refs.paging.reload()//重置
			},
			// 切换标签选择状态
			toggleTagSelect(tag) {
				const index = this.selectedTags.findIndex(t => t.id === tag.id);
				if (index >= 0) {
					// 已选中，取消选择
					this.selectedTags.splice(index, 1);
				} else {
					// 未选中，添加到已选
					this.selectedTags.push(tag);
				}
			},
			// 检查标签是否已选中
			isTagSelected(tag) {
				return this.selectedTags.some(t => t.id === tag.id);
			},
			// 确认选择
			confirmSelection() {
				// 保存到本地存储
				// uni.setStorageSync('selectedTags', JSON.stringify(this.selectedTags));
				let selectedTagsJson = JSON.stringify(this.selectedTags);
				// 返回上一页
				setTimeout(() => {
					uni.navigateBack();
					uni.$emit('htmlRefresh', {selectedTagsJson: selectedTagsJson});
				}, 600);
				// uni.navigateBack();
			},
			moreClick(e){
				console.log(e)
			},
			// 获取列表
			queryList(pageNo, pageSize) {
				
				let params = {};
				params.page = pageNo;
				params.keyword = this.keyword;
				request.postWithToken({
					url: '/merchant/getGoodsCategory',
					data: params
				}).then(res=>{
					// console.log(res)
					// this.$refs.paging.complete(res.data);
					this.filteredTags = [...res.data];
					this.$refs.paging.complete(this.filteredTags);
				})
			},
			followClick(e){
				let talkData = uni.getStorageSync('talkData');
				
			},
			jumpTo(path){
				uni.navigateTo({
					url: path
				})
			},
			onSubmitSave(){
				uni.navigateTo({
					url: '/pages/prescription/selectMedicine'
				})
			}
		}
	}
</script>

<style>
	page{
		background-color: #f2f2f2 !important;
	}
</style>
<style lang="scss" scoped>
	
</style>
